using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

public class WallEditor
{
    private string[] m_wallList;
    private int m_curWall = 0;
    int nStartX = 0;
    int nStartZ = 0;
    int nEndX = 0;
    int nEndZ = 0;
    
    public void Init()
    {        
        uint nCount = DBCManager.GetInstance().GetWallDBC().GetRecordCount( );
        m_wallList = new string[nCount];
        for(uint i = 0; i < nCount; i++)
        {
            m_wallList[i] = DBCManager.GetInstance().GetWallDBC().GetString(i, 0);
        }
    }

    public void OnGUI()
    {
        m_curWall = EditorGUILayout.Popup("墙名称", m_curWall, m_wallList);
    }

    public void OnSceneGUI(SceneView sceneview)
    {        
        int controlID = GUIUtility.GetControlID( FocusType.Passive);
        if(Event.current.type == EventType.Layout)
        {
            HandleUtility.AddDefaultControl(controlID);
        }
        Ray worldRay = HandleUtility.GUIPointToWorldRay(Event.current.mousePosition);
        RaycastHit hitInfo;

        if (Physics.Raycast(worldRay, out hitInfo, 10000.0f, GridManager.mGridListLayerMask))
        {                
            int nIndexX = (int)hitInfo.point.x;
            int nIndexZ = (int)hitInfo.point.z;
            
            if(Event.current.type == EventType.MouseMove)
            {// 未按键移动
                MyBuildingTool.selObj.transform.position = new Vector3((float)nIndexX+0.5f,0,(float)nIndexZ+0.5f);
            }
            else if (Event.current.type == EventType.MouseDrag && Event.current.button == 0)
            {// 左键按下拖动
                nEndX = nIndexX;
                nEndZ = nIndexZ;
                int nTStartX, nTEndX;
                int nTStartZ, nTEndZ;
                if(nStartX < nEndX)
                {
                    nTStartX = nStartX;
                    nTEndX = nEndX;
                }
                else
                {
                    nTStartX = nEndX;
                    nTEndX = nStartX;
                }
                if(nStartZ < nEndZ)
                {
                    nTStartZ = nStartZ;
                    nTEndZ = nEndZ;
                }
                else
                {
                    nTStartZ = nEndZ;
                    nTEndZ = nStartZ;
                }
                int distX = nTEndX-nTStartX;
                int distZ = nTEndZ-nTStartZ;
                if(distX >= distZ)
                {
                    MyBuildingTool.selObj.transform.position = new Vector3((nEndX+nStartX)*0.5f+0.5f,0,(float)nStartZ+0.5f);
                    MyBuildingTool.selObj.transform.localScale = new Vector3(distX+1,0.1f,1);
                }
                else
                {
                    MyBuildingTool.selObj.transform.position = new Vector3((float)nStartX+0.5f,0,(nEndZ+nStartZ)*0.5f+0.5f);
                    MyBuildingTool.selObj.transform.localScale = new Vector3(1,0.1f,distZ+1);
                }
            }
            else if (Event.current.type == EventType.MouseDown && Event.current.button == 0)
            {// 点击左键
                MyBuildingTool.selObj.transform.position = new Vector3((float)nIndexX+0.5f,0,(float)nIndexZ+0.5f);
                MyBuildingTool.selObj.transform.localScale = new Vector3(1,0.1f,1);
                nStartX = nIndexX;
                nStartZ = nIndexZ;
                nEndX = nStartX;
                nEndZ = nStartZ;
            }
            else if(Event.current.type == EventType.MouseUp && Event.current.button == 0)
            {
                int nTStartX, nTEndX;
                int nTStartZ, nTEndZ;
                if(nStartX < nEndX)
                {
                    nTStartX = nStartX;
                    nTEndX = nEndX;
                }
                else
                {
                    nTStartX = nEndX;
                    nTEndX = nStartX;
                }
                if(nStartZ < nEndZ)
                {
                    nTStartZ = nStartZ;
                    nTEndZ = nEndZ;
                }
                else
                {
                    nTStartZ = nEndZ;
                    nTEndZ = nStartZ;
                }
                int distX = nTEndX-nTStartX;
                int distZ = nTEndZ-nTStartZ;
                if(distX >= distZ)
                {
                    for(int x = nTStartX; x < nTEndX+1; x++)
                    {
                        if(GridManager.GetInstance().CanAddObj(ObjType.eot_Wall, 1, 1, x, nStartZ))
                        {
                            GridManager.GetInstance().AddBuilding(ObjType.eot_Wall, m_wallList[m_curWall], x, nStartZ);
                        }
                    }
                }
                else
                {
                    for(int z = nTStartZ; z < nTEndZ+1; z++)
                    {
                        if(GridManager.GetInstance().CanAddObj(ObjType.eot_Wall, 1, 1, nStartX, z))
                        {
                            GridManager.GetInstance().AddBuilding(ObjType.eot_Wall, m_wallList[m_curWall], nStartX, z);
                        }
                    }
                }
                MyBuildingTool.selObj.transform.localScale = new Vector3(1,0.1f,1);
            }
        }
    }
}